﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CompanyBussiness;

namespace Website.admin.Control
{
    public partial class cListNews : System.Web.UI.UserControl
    {
        NewsManagement newsMana = new NewsManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        CompanyBussiness.New news=new New();
        CheckValid cv = new CheckValid();
        public int CurPage=1;
        int TotalRow;
        int TotalPage;
        int StartRow = 0;
        int categoryIDs;
        string modulCode;

        protected void Page_Load(object sender, EventArgs e)
        {
            fail.Visible = false;
            success.Visible = false;
            //bindDropDownListFromDateTime(ddlFromHour, "h", false);
            //bindDropDownListFromDateTime(ddlFromMinute, "m", false);
            //BindDropDownListToDateTime(ddlToHour, "h", true);
            //BindDropDownListToDateTime(ddlToMinute, "m", true);
            //bdFromDate.SelectedDate = DateTime.Now;
            //bdToDate.SelectedDate = DateTime.Now;
            modulCode = Request.QueryString["Mod"];
            loadModul();

            if (!Page.IsPostBack)
            {
                if (Request[CONST.QUERYSTRING_CATEGORY] == null)
                {
                    Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CategoryNews";
                    Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CategoryNews&Mod="+ modulCode);
                }
                else
                {
                    categoryIDs = int.Parse(Request[CONST.QUERYSTRING_CATEGORY].ToString());
                    lbListName.Text = newsMana.GetCategoryByID(categoryIDs,modulCode).CategoryName.ToString();
                    ContentSearch();
                }
            }
            
               
        }

        protected void loadModul()
        {
            switch (modulCode)
            {
                case "MNEWS":
                    {
                        tableNews.Visible = true;
                    }
                    break;
                case "MSHAREHOLDERS":
                    {
                        tableNews.Visible = false;
                    }
                    break;
                case "MJOB":
                    {
                        tableNews.Visible = false;
                    }
                    break;
                case "MHELP":
                    {
                        tableNews.Visible = false;
                    }
                    break;
                case "MCONTACT":
                    {
                        tableNews.Visible = false;
                    }
                    break;
                case "MINTRO":
                    {
                        tableNews.Visible = false;
                    }
                    break;
            }


        }

        protected int GetRowinPage()
        {
            return int.Parse(dropRows.SelectedValue.ToString());
        }

        protected void LoadGridView(New news,int take)
        {
            try
            {
               
                if (Request["Page"] == null)
                {
                    CurPage = 1;
                }
                else
                {
                    CurPage = Convert.ToInt32(Request["Page"]);
                }

                dropChoicePage.SelectedValue = CurPage.ToString();

                if (CurPage > 1)
                {
                    StartRow = (CurPage - 1) * 10;
                }
                else
                {
                    StartRow = 0;
                }

                //if (modulCode == "MNEWS")
                //{
                //    GridViewListNews.DataSource = newsMana.GetNewsSearch(news, GetFromDate(), GetToDate(), take, StartRow);
                //    GridViewListNews.DataBind();
                //    TotalRow = newsMana.GetNumberNewsSearch(news, GetFromDate(), GetToDate());
                //}
                //else
                //{
                    GridViewListNews.DataSource = newsMana.GetNewsSearchNotDate(news, take, StartRow);
                    GridViewListNews.DataBind();
                    TotalRow = newsMana.GetNumberNewsSearchNotDate(news);
                //}
                
                if (TotalRow == 0)
                {
                    divpagination.Visible = false;
                    fail.Visible = true;
                    btnSave.Visible = false;
                    btnCancel.Visible = false;
                    lbError.Text = "Không có bài viết nào!";
                    return;
                }
                else
                {
                    pnPagination.Visible = true;
                    conditionsRight.Visible = true;
                    fail.Visible = false;
                    success.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                }

                this.Pagination(take);
            }
            catch (Exception)
            { 
            
            }            
        }

        protected void Pagination(int take)
        {
            try
            {
                if (TotalRow % take == 0)
                    TotalPage = TotalRow / take;
                else
                    TotalPage = (TotalRow / take) + 1;

                dropChoicePage.Items.Clear();
                for (int i = 0; i < TotalPage; i++)
                {
                    dropChoicePage.Items.Add((i + 1).ToString());
                }


                if (CurPage > 1)
                {
                    lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=CreateCategory&" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(1);
                    lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=CreateCategory&" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(CurPage - 1);
                }
                if (CurPage < TotalPage && CurPage > 1)
                {
                    lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=CreateCategory&" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(TotalPage.ToString());
                    lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=CreateCategory&" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(CurPage + 1);
                }


                numCurrent.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=CreateCategory&" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(CurPage);
                numBefore.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=CreateCategory&" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(CurPage - 1);
                numAfter.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=CreateCategory&" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(CurPage + 1);

                if (CurPage == 1 && TotalPage == 1)
                {
                    numCurrent.Text = CurPage.ToString();
                    numBefore.Text = "";
                    numAfter.Text = "";
                    numBefore.Visible = false;
                    numAfter.Visible = false;
                }
                else if (CurPage == 1 && TotalPage > 1)
                {
                    numCurrent.Text = CurPage.ToString();
                    numBefore.Text = "";
                    numBefore.Visible = false;
                    numAfter.Text = Convert.ToString(CurPage + 1);
                }
                else if (1 < CurPage && CurPage < TotalPage)
                {
                    numCurrent.Text = CurPage.ToString();
                    numBefore.Text = Convert.ToString(CurPage - 1);
                    numAfter.Text = Convert.ToString(CurPage + 1);
                }
                else if (CurPage == TotalPage)
                {
                    numCurrent.Text = CurPage.ToString();
                    numBefore.Text = Convert.ToString(CurPage - 1);
                    numAfter.Text = "";
                    numAfter.Visible = false;
                }
            }
            catch (Exception)
            { 
            
            }
        }

        protected void dropChoicePage_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurPage = Convert.ToInt32(dropChoicePage.SelectedValue.ToString());
            Response.Redirect(Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=CreateCategory&" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(CurPage) + "&Mod=" + modulCode);
        }

        protected void btnEdit_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "EditNews")
            {
                Session[CONST.SESSION_NEWS] = Convert.ToInt32(e.CommandArgument);
                try
                {
                    Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CreateNews";
                    Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "UpdateNews";
                    Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateNews&" + CONST.QUERYSTRING_NEWS + "=" + int.Parse(Session[CONST.SESSION_NEWS].ToString())+"&Mod="+modulCode);
                }
                catch (Exception)
                {

                }
            }
        }
  
        protected void linkCategoryNews_Click(object sender, EventArgs e)
        {
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CategoryNews";
            Response.Redirect("Administrator.aspx?"+CONST.ACTION+"=CategoryNews&Mod="+modulCode);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
               
                ContentSearch();
            }
            catch (Exception)
            { 
            
            }        
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            btnCancel.Enabled = false;
            StartRow = CurPage * 10;
            for (int i = 0; i < GridViewListNews.Rows.Count; i++)
            {
                news = newsMana.GetNewsByID(int.Parse(GridViewListNews.DataKeys[i].Value.ToString()));
                bool status = ((CheckBox)GridViewListNews.Rows[i].FindControl("checkStatus")).Checked;
                newsMana.UpdateNews((int)news.NewsID, news.NewsSubject, news.NewsIntro, news.NewsContent, news.NewsAuthor, news.NewsSource, news.NewsLink, DateTime.Now,(int)news.NewsAccess, news.NewsImage, status,news.FileAttach,(bool)news.ImagesStatus, (int)news.CategoryID);
            }
           
            ContentSearch();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                ContentSearch();
            }
            catch (Exception)
            { 
            
            }
        }

        protected void ContentSearch()
        {
            categoryIDs = int.Parse(Request[CONST.QUERYSTRING_CATEGORY].ToString());

            string error = string.Empty;

            //if (CheckValidDate(ref error))
            //{
                news.CategoryID = categoryIDs;

                news.NewsSubject = txtSearch.Text;
               
                if (dropStatus.SelectedValue.ToString() == "true")
                {
                    news.NewsStatus = true;
                }
                else if (dropStatus.SelectedValue.ToString() == "false")
                {
                    news.NewsStatus = false;
                }
                
                news.NewsAuthor = txtAuthor.Text;
                
                this.LoadGridView(news, int.Parse(dropRows.SelectedValue.ToString()));
            //}
            //else
            //{
            //    lbError.Text = error;
            //    fail.Visible = true;
            //    return;
            //}
        }

        protected void dropRows_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ContentSearch();
            }
            catch (Exception)
            { 
            
            }        
        }

        private static void bindDropDownListFromDateTime(DropDownList ddlControl, string option, bool getNow)
        {
            switch (option)
            {
                case "h":
                    {
                        var li1 = new ListItem("Giờ", string.Empty);
                        ddlControl.Items.Insert(0, li1);
                        for (int i = 0; i <= 23; i++)
                        {
                            var li = new ListItem(i.ToString(), i.ToString());
                            ddlControl.Items.Add(li);
                        }
                        if (getNow)
                        {
                            ddlControl.SelectedValue = DateTime.Now.Hour.ToString();
                        }
                        else
                        {
                            ddlControl.SelectedIndex = 1;
                        }
                        break;
                    }
                case "m":
                    {
                        var li1 = new ListItem("Phút", string.Empty);
                        ddlControl.Items.Insert(0, li1);
                        for (int i = 0; i <= 59; i++)
                        {
                            var li = new ListItem(i.ToString(), i.ToString());
                            ddlControl.Items.Add(li);
                        }
                        if (getNow)
                        {
                            ddlControl.SelectedValue = DateTime.Now.Minute.ToString();
                        }
                        else
                        {
                            ddlControl.SelectedIndex = 1;
                        }
                        break;
                    }
                default:
                    {
                        var li1 = new ListItem("Chọn", string.Empty);
                        ddlControl.Items.Insert(0, li1);
                        ddlControl.SelectedIndex = 0;
                        break;
                    }
            }
        }

        private static void BindDropDownListToDateTime(ListControl ddlControl, string option, bool getNow)
        {
            switch (option)
            {
                case "h":
                    {
                        var li1 = new ListItem("Giờ", string.Empty);
                        ddlControl.Items.Insert(0, li1);
                        for (int i = 0; i <= 23; i++)
                        {
                            var li = new ListItem(i.ToString(), i.ToString());
                            ddlControl.Items.Add(li);
                        }
                        if (getNow)
                        {
                            ddlControl.SelectedValue = DateTime.Now.Hour.ToString();
                        }
                        else
                        {
                            ddlControl.SelectedIndex = 0;
                        }
                        break;
                    }
                case "m":
                    {
                        var li1 = new ListItem("Phút", string.Empty);
                        ddlControl.Items.Insert(0, li1);
                        for (int i = 0; i <= 59; i++)
                        {
                            var li = new ListItem(i.ToString(), i.ToString());
                            ddlControl.Items.Add(li);
                        }
                        if (getNow)
                        {
                            ddlControl.SelectedValue = DateTime.Now.Minute.ToString();
                        }
                        else
                        {
                            ddlControl.SelectedIndex = 0;
                        }
                        break;
                    }
                default:
                    {
                        var li1 = new ListItem("Chọn", string.Empty);
                        ddlControl.Items.Insert(0, li1);
                        ddlControl.SelectedIndex = 0;
                        break;
                    }
            }
        }

        //private DateTime GetFromDate()
        //{
        //    string time = ddlFromHour.SelectedValue + ":" + ddlFromMinute.SelectedValue + ":00";
        //    string date = bdFromDate.SelectedDate.ToShortDateString();
        //    DateTime result = DateTime.Parse(date + " " + time);
        //    return result;
        //}

        //private DateTime GetToDate()
        //{
        //    string time = ddlToHour.SelectedValue + ":" + ddlToMinute.SelectedValue + ":00";
        //    string date = bdToDate.SelectedDate.ToShortDateString();
        //    DateTime result = DateTime.Parse(date + " " + time);
        //    return result;
        //}

        //private bool CheckValidDate(ref string error)
        //{
        //    error = string.Empty;
        //    if (ddlFromHour.SelectedValue == string.Empty)
        //    {
        //        error += "<li>Bạn chưa chọn giờ ngày bắt đầu</li>";
        //    }
        //    if (ddlFromMinute.SelectedValue == string.Empty)
        //    {
        //        error += "<li>Bạn chưa chọn phút cho ngày bắt đầu</li>";
        //    }
        //    if (ddlToHour.SelectedValue == string.Empty)
        //    {
        //        error += "<li>Bạn chưa chọn giờ ngày kết thúc</li>";
        //    }
        //    if (ddlToMinute.SelectedValue == string.Empty)
        //    {
        //        error += "<li>Bạn chưa chọn phút cho ngày kết thúc</li>";
        //    }
        //    if (bdFromDate.SelectedValue == null)
        //    {
        //        error += "<li>Bạn chưa chọn ngày bắt đầu</li>";
        //    }
        //    if (bdToDate.SelectedValue == null)
        //    {
        //        error += "<li>Bạn chưa chọn ngày kết thúc</li>";
        //    }
        //    if (!error.Equals(string.Empty))
        //    {
        //        return false;
        //    }
        //    DateTime fromDate = GetFromDate();
        //    DateTime toDate = GetToDate();

        //    if (fromDate > toDate)
        //    {
        //        error = "Khoảng thời gian bạn chọn không hợp lệ";
        //        return false;
        //    }
        //    return true;
        //}
    }
}